Looking at this list, it seems like most sovereign immunity laws exist in countries with monarchies, which makes sense both historically (an artifact of the principle of rex non potest peccare); and pragmatically, as usually in these countries the monarchs have little actual power, or else the people don't have the power to compel them to cede this privilege.
What I don't understand is that many republics grant this power in their constitution to an elected head of state. Examples from that list are Italy, Sri Lanka, and Singapore. Similarly the President of France enjoys immunity from prosecution while in office.
What is the reason to make such a law? Wouldn't it invite corruption and lack of accountability? Are there any advantages of it to the state?