However, in a lot of situations, this immunity is waived by statute to
allow for people to sue states and the federal government for damages.
My question is what is the rationale behind this? Why have immunity
except only to waive it? Would a republic or its government be weaker
if this immunity didn't exist?
The primary issue is that the influence of government is so pervasive that almost anyone could sue the government over almost anything on some plausible theory, making the state ungovernable.
Typically, the government waives governmental immunity for liability for negligence of the predominant types asserted against private entities (e.g. slip and fall, premises liability in government buildings, and automobile accident liability), while retaining immunity for intentional acts arising in tort (i.e. not in contract, or in equity, or by statute).
Statutes establish what kind of additional liability is allowed, with intentional violations of civil rights, discrimination claims of government employees, and takings of property without just compensation typically being the main categories.
Some countries, like France, start from the opposite direction, of "rule of law" and full governmental liability for misconduct or harm, subject to exceptions that protect discretion in government decision making.
It would certainly be possible to broaden the scope of government liability in the United States without doing undue harm (e.g. by narrowing the scope of the common law "state secrets doctrine", or allowing for civil liability of prosecutors and judges in cases where criminal liability or ethical violations have already been established as a matter of law, or by allowing for recoveries for civil rights violations without regard to the intent of the people causing them).
But there also needs to be some substantive limitation on governmental liability so that liability for harm in "failure to regulate" and "failure to warn" claims, for example, don't cause courts to largely displace political decision making. The problem is illustrated by pro se prisoner's petitions in the U.S. which bring up some legitimate grievances, but for every one raised, bring up hundred of collateral attacks on convictions or complaints about prison conditions that don't have a legal basis and are often substantively without factual merit.
Historically, part of the problem is that "public law" (i.e. the law relating to the conduct of government) in the United States is an outgrowth of "private law" lawsuits between private individuals in a tradition that has been rather ad hoc and has not been viewed comprehensively as a problem to be addressed systemically and holistically from the ground up. In contrast, most civil law countries have separate courts, institutions, and bodies of law designed to specifically address public law issues in ways that reflect its uniqueness and differences from private law.
The French Council of State is often viewed as a paradigm of good public law practice.
In this system, any person can fill out a widely available complaint form, and if the complaint meets minimum standards of merit, the Council of State assigns a senior civil servant trained in public law to be an advocate for the citizen, and a senior civil servant from the same poll to defend the state.
The Council of State process is designed so that, when it is working at its best, in situations where many people are impacted by a problematic governmental practice, that the flawed institution must be corrected and every discernible material victim of the flawed institution should receive a remedy, rather than simply providing a remedy to the one individual who filed a complaint.
Individual aggrieved citizens don't bear the litigation costs of the process, but the devil's advocates appointed in this process are, in effect, a "loyal opposition" who seek to make the government better by correcting its flaws, rather than looking for reasons to unreasonably gum up the system, or press political advantages for the complaining parties when the merits of the complaint are weak on the facts or the law.