From a human perspective, God's behavior towards Pharaoh can definitely seem confusing. How could a just and merciful God do something that seems so unfair? But according to the Bible, God is perfectly just and merciful. He does not make mistakes. When we consider the story with those qualities in mind, there are a few explanations that can help us grapple with his actions.
First, we can consider that if we believe God is the supreme creator and ruler of the universe, he is within his rights to dispense justice as he sees fit. The Pharaoh was by no means a godly or upright man and was abusing the Israelites, God's chosen people. If we consider the laws God gave in the Old Testament, he would technically be justified in destroying all of sinful Egypt. While the plagues are certainly unpleasant, in some ways it was merciful that he punished Egypt without destroying them. Hardening the Pharaoh's heart was part of this punishment.
Second, we can note that there are places where the Pharaoh chose to have a hard heart independently. In Exodus 8:15, it states that "But when Pharaoh saw that there was relief, he hardened his heart." In Exodus 8:32 it happens again, noting that "But this time also Pharaoh hardened his heart and would not let the people go." Christians have had a lot of different ideas (and arguments!) about how free will works, but here we can assume that God's choice to harden the Pharaoh's heart may have been in line with the Pharaoh's own will and choices. He was already heading down a hard-hearted path.
In the New Testament, some people had the same concerns as you. The Apostle Paul responded to their fears in Romans 9 by writing:
"What then shall we say? Is God unjust? Not at all! For he says to Moses, 'I will have mercy on whom I have mercy, and I will have compassion on whom I have compassion.' It does not, therefore, depend on human desire or effort, but on God’s mercy. For Scripture says to Pharaoh: 'I raised you up for this very purpose, that I might display my power in you and that my name might be proclaimed in all the earth.' Therefore God has mercy on whom he wants to have mercy, and he hardens whom he wants to harden. One of you will say to me: 'Then why does God still blame us? For who is able to resist his will?' But who are you, a human being, to talk back to God? Shall what is formed say to the one who formed it, ‘Why did you make me like this?’"
Paul sounds a little terse when he tells us "Who are you to question God?" But while his message can sound a little harsh, ultimately he is telling Christians to trust God. God has told us that he is just and merciful, and he showed us that he was so when he sacrificed his son Jesus for all of our sins. It may be hard to understand his mercy when we read certain parts of the Bible or see strife in the world, but we have to have faith that he knows what he's doing (even if we don't!) Our salvation in Jesus is proof that God loves the world and has a plan for us all.
I hope this is helpful. Let me know if you have anymore questions about the Bible or Christian beliefs!