Punishment is part of love, if we do not receive punishment when we act badly we feel free to continue doing evil and then it becomes a habit in our lives, when we realize we have already become evil beings and away from God.
pixabay
However, when we act badly and are reprimanded we put a stop to our bad deeds, if we recognize that we have done badly we will feel sad and even more if we are punished, because the process does not bring joy with it but a moment of reflection in which we must think about what we did, once we have passed the test then we are new people, with greater maturity and more care.
bible.com
God punishes those He loves because it is His way of warning them not to be lost in eternal punishment. Everyone tends to forget about God when they have abundance and no need and everyone remembers God when they are in trouble and need, then it is a Law, if we forget about God problems and needs will come so that we can remember Him again. That is why it is better to keep Him in mind every day, not because this way there will be no problems, but with Him it will be easier to get out of them.