Hamas is a proxy of Iran.
I made an earlier comment about how countries like the US love to blow up things but they're not as good about planning for the day after the bombs.
The same thing is true of Israel. In the 20th century, Israel created a power vacuum in Lebanon, particularly in south Lebanon. This led to the rise of Hezbollah, which is a Shi'a group sponsored by Iran. What makes these organizations so different from the Western concept of "government" is that groups like Hezbollah are religious groups that function as a promoter of Islam, a charity and a political party.
Israel's displacement of the Palestinians after WWI and the fall of the Ottoman Empire set the stage for another power vacuum that neighboring countries took advantage of. Hamas is nominally Sunni, and like Hezbollah, it is a promoter of Islam, a charity and a political party. Hamas started as an offshoot of the Muslim Brotherhood which was underwritten by Sunnis in Egypt but more recently, Hamas has received financial support from UAE (Sunni), Qatar (Sunni) and, notably, Iran (Shi'a).
What Israel is doing is trying to remove Iran's influence in Lebanon and Gaza. The path to do this is to attack Iran's proxies, Hezbollah and Hamas, and now Israel has decided to take on Iran directly. Israel has also succeeded in dragging the US into it by taking advantage of America's bad history with iran.
Why? There's a lot of reasons but a big reason is that Netanyahu and the Ultra-Orthodox Jewish coalition that keeps Netanyahu in power see it as their path to remaining in power in Israel. Netanyahu had been underwriting Hamas and allowing financial support from neighboring countries to pour into Hamas. Netanyahu was hoping that by financially supporting Hamas, it would weaken the PLO which is the controlling party in the West Bank. This blew up in Netanyahu's face on October 7. Now, in order to keep in power, he has to cover up his history with Hamas with hyper-aggressive actions in Lebanon, Gaza and Iran. Netanyahu has succeeded- his popularity in Israel has risen since he started the war in Gaza and with Iran, as many Israelis have felt that Hamas and Hezbollah (and Iran) are threats to their feeling of security.
All of these countries have something in common- they are becoming more and more religiously conservative, and those religious conservatives want to remain in power and will do anything to hold on to power.