It is analogous to a painter slashing his own painting.
We would think a painter quite insane if he did such a thing so why do you not think God insane for punishing us for being exactly what he created us to be?
All of the great artists are a little insane. They see things that the rest of us do not.
Maybe a better analogy would be a AI machine that was created by an engineer. Say that he wants to pressure the machine into behaving a certain way and the best way to do that is to cause the machine some pain. So the engineer programs the machine to feel pain whenever it does some unacceptable thing.
What is immoral about that?