This video by a political science professor explains it best: https://youtu.be/zMxHU34IgyY?si=N5oHElN4Xlbiqznh
In short, the only people who truly know are Hamas, and the best the rest of us can do is speculate.
Some possibilities are that Hamas wanted to sabotage normalizing relations between Israel and the rest of the Muslim world, that Hamas wanted to bait Israel into a wildly disproportionate response that would garner themselves sympathy and recruits, that Hamas was bluffing and feigning strength and counting on Israel to think the attack was bait, that Hamas was just acting on bloodlust and wanted to attack regardless of the consequences, or many other possibilities.
Further, we focus a lot on the substative issues, i.e., the grievances and disagreements at hand, but we don't talk about the bargaining frictions nearly enough. There are countless border disputes around the world, and yet they rarely result in war. Why? Because war is costly and most wish to avoid it. War typically happens when there are both substantive issues and bargaining frictions, i.e., things preventing the two sides from negotiating a solution. But us onlookers can't even know for sure what these frictions are, only speculate.
All this is simply the nature of the fog of war, that the true strategies/goals won't be known for a while, if ever. Anyone who tries to tell you with certainty why they did what they did at this stage doesn't actually know with any degree of certainty. Nobody but Hamas actually knows.
I do recommend watching the full video above, as the professor is very engaging, rather amusing, and covers this topic quite thoroughly.