So TL;DR:
Why would countries attack civilian facilities like hospitals during war?
Most common reason when dealing with first world countries is that the facility is no longer a hospital but a military location. (some examples are given below)
Another reason is that it is actually taken over by combatants whether in its entirety or with human hostages as shields.
The other reasons that then appear are mistakes. (some examples are given below)
Another reason is that it is part of war. It is part of what happens.
(I mention first world countries as when this affects them, a stance is taken, when it is not such a country then, sometimes, heads are looking the other way, as in Syria and other non-first world locations, and so appears to happen more often in these regions)
Why would countries attack civilian facilities like hospitals during war?
Even schools are not safe from this - sometimes listed as military locations as well, and for the same reasons listed above.
https://childrenandarmedconflict.un.org/six-grave-violations/attacks-against-schools/
The other thing, related, perhaps to being a first world country or not - is the attitude given to Geneva Conventions - some might regard them as little more than inconvenient: In war, anything goes.
If you are sure you will never be held accountable then you be emboldened to carry out such attacks.
Why would countries in similar situations do that?
Some would, some would not.
It is no mistake that those of First World military planners would take into account the Geneva Conventions, the real threat of being convicted for War Crimes, the fear of being dismissed dishonorably from service, etc, these are real considerations and measures like lists and coordinates of no-strike zones or no-targets or facilities are actively drawn up and distributed.
But these measures are by no means fool proof, especially in war, as the US attack in Kunduz investigation shows (listed below).
But some other countries, regions, regimes, despots, etc, would not give the same consideration.
In a so called asymmetric war, one side might be desperate enough to relax its attitude on potential war crimes - desperate times require desperate measures, 'leave the consequences for later, we are fighting for our lives.'
Is it simply a mistake?
Sometimes yes, examples shown below.
Or what would the attacker hope to gain by an action that would presumably be condemned by many other countries in the world?
Again, taking in to account the above reasoning, it is something that happened in desperate times; or the reason was real, it really was a military location; or used as a human shield but legitimate anyway; or it is just part of how they fight their war - they do not have the military materiel luxury that a major power might enjoy and so have to use what they can, how they can, where they can, and sometimes regardless of collateral damage.
Some countries may simply not care for the opinions of others. Their country, their conflict, their reasons are their own.
This is of course a global question, and how people fight in different regions are sometimes very different to that standard we hold ourselves to.
....
So the following are examples with more details and includes the Russian response as well as past events from both US and Russia, whether mistakes or not.
..........
(My other answer was deleted but I thought it was valid in terms that in the case of):
Why would countries attack civilian facilities like hospitals during war?
In the current case of the Russian attack, one of their claims is:
...the hospital building had for days been under the control of ultra-radical Ukrainian forces who had emptied out the doctors and patients
https://www.reuters.com/world/europe/russia-says-claim-that-it-bombed-childrens-hospital-are-fake-news-2022-03-10/
They claim that they warned on March 7, claiming that the hospital was being used by the Azov battalion.
As stated elsewhere, when a location is identified (whether falsely or not) as legitimate military location, it becomes a legitimate military target.
Why would countries in similar situations do that?
If done with intent, the reason would usually be that it is no longer a hospital and/or it is being used as a military facility perhaps with human shields.
Whether or not collateral damage in that event can be contained or not should, however, be a factor.
Is it simply a mistake?
As in the Belgrade example they did admit to a mistake, whether ont he part of the unit carrying out the attack or the intelligence that made it a target. However it was admitted and an apology made.
Another US mistake:
Was Hospital A Taliban Human Shield? Doctors Without Borders Denies Afghan Allegations After Deadly Airstrike In Kunduz
https://www.ibtimes.com/was-hospital-taliban-human-shield-doctors-without-borders-denie
After apparent U.S.-led airstrikes in Afghanistan devastated a hospital run by the international humanitarian organization Doctors Without Borders and killed at least 19 people Saturday, Afghan Defense Ministry officials alleged Taliban militants had been using the hospital as the equivalent of a human shield and directly firing on American and Afghan armed forces.
The acting governor of the Kunduz province called the Doctors Without Borders hospital a “Taliban base.” Hamdullah Danishi said in an interview with the Washington Post that “the hospital campus was 100 percent used by the Taliban.”
The accusation that a "war crime has been committed" came as U.S. defense officials pledged to look into U.S. involvement in the airstrike.
Three investigations of the incident were conducted by NATO, a joint United States-Afghan group, and the United States Department of Defense.
On 7 October 2015, President Barack Obama issued an apology and announced the United States would be making condolence payments of $6,000 to the families of those killed in the airstrike
results of ...internal investigation ...described the incident as "the direct result of avoidable human error, compounded by process and equipment failures." :
- the AC-130 gunship crew misidentified the clinic as a nearby Taliban-controlled government building.
- The American gunship had identified the building based on a visual description from Afghan troops, and did not consult their no-strike list, which included the coordinates of the hospital as provided by Medecins Sans Frontieres.
- Electronic equipment malfunctions on the gunship prevented it from accessing email and images, while a navigation error meant its targeting equipment also misidentified the target buildings.
- A final report by the Pentagon, released 29 April 2016, reaffirmed the incident as an accident, and said it thus did not amount to a war crime.
Or what would the attacker hope to gain by an action that would presumably be condemned by many other countries in the world?
In other cases:
Should the facility be revealed to be a legitimate target the attacker would feel vindicated.
Otherwise, it is a mistake that is covered up, or it is seen as legitimate target from their point of view, and that is the only pov that matters to them.
Military Attacks on “Hospitals Shields”: The Law Itself is Partly to Blame
https://www.justsecurity.org/33712/military-attacks-hospitals-shields-law-partly-blame/
From the war in Afghanistan and the US-backed Saudi intervention in Yemen, to the Israeli occupation of Palestine and the Syrian civil war, hospitals have increasingly been targeted by military forces. The justification for many of these attacks has been uncannily similar: the hospitals were bombed because they were shielding combatants and therefore the attacks do not constitute a violation of international law. Hospitals, in other words, are now classified as if they are equivalent to human shields.
During its 2014 war on Gaza, Israel bombed different Palestinian medical facilities, destroying parts of one hospital and 5 primary health care centers. In an attempt to defend its strikes, Israel accused Hamas of using hospitals to store weapons and hide armed militants.
after the recent bombardment of an underground medical facility in a rebel controlled area, a Syrian regime official declared that militants would be targeted wherever they were found, “on the ground and underground,” while his Russian patron explained that rebels were using “so-called hospitals as human shields.”
Saudi officials attempting to justify the high number of air strikes targeting medical facilities have adopted the same catchphrases. They, too, accused their adversaries, the Houthi militias, of using hospitals to hide their military forces.
when health care facilities become “hospital shields” they lose the protected status they are granted by the Geneva Conventions. Thus, once framed as shields, these facilities can be bombarded without violating international law.
International law prohibits the use of civilians as human shields to protect military targets, but it also permits the attacking forces to kill human shields as long as they abide by the principle of proportionality.
By extension, if hospitals are used as shields, they too can be bombed provided the principle of proportionality is not breached.
international law affirms that the protection to which hospitals are entitled is revoked when they are “used to commit, outside their humanitarian duties, acts harmful to the enemy."
This article claims that there is a history of targeted attacks on hospitals, by the US:
Look for Hospitals as Targets
https://www.thenation.com/article/archive/look-for-hospitals-as-targets/
“Bombing of Hospitals Called Routine.” That was the August 9, 1973, Newsday coverage of congressional hearings on “clandestine U.S. air and ground activities in Cambodia and Laos”:
U.S. commanders in Vietnam placed no restrictions on ground or air attacks against Viet Cong or North Vietnamese hospitals a Senate committee was told yesterday by several Vietnam veterans.
In testimony on the hospitals, Alan Stevenson, a stockbroker from San Francisco and former Army intelligence specialist, said that while in Quang Tri province in 1969, he routinely listed hospitals among targets to be struck by American fighter plans. (sic)
“The bigger the hospital, the better it was,” he said. Stevenson said he believed the hospitals were rated highly as potential targets, not because American commanders wanted to attack wounded enemy troops, but because hospital complexes were often protected by company- or battalion-sized troop units….
Other examples:
Russia in Syria:
https://news.sky.com/story/ukraine-war-why-russia-is-bombing-hospitals-in-ukraine-and-how-putin-has-done-it-before-12562152
Putin has done this before.
The biggest example was in Idlib, Syria in 2019 where 24 hospital facilities were targeted first - before any other military action.
The International Red Cross and other members of the aid community had communicated the coordinates of the hospitals to make sure they would be left alone.
But instead the Russians used those very coordinates to bomb them. So after 2019 the aid community had to stop doing that.
Why has Russia done it?
It's an attempt to create terror in the population and to break civilian morale.
In Mariupol, they just want the city to give in.
Usually they offer people a way out, but they aren't offering any genuine escape routes in Mariupol - they just want them to surrender.
Essentially, it's a medieval siege.
only around 10% of Russian weapons are precision weapons.
So they are a lot more likely than us to say they hit the hospital buildings by accident.
hitting hospitals drives people out.
If you get injured in the fighting, it's very hard to get treated if there aren't any hospitals, so people leave and go elsewhere.
It's a deliberate policy of displacement - so they can empty parts of a city they want to occupy.
.......
Going back to the current topic of the hospital in Mariupol I have yet to find a Russian report of the hospital being taken over.
What I have found are news items from the hospital that do seem to show it operating as such as recently as March 6:
https://windobi.com/mariupol-diary-scenes-from-ukrainian-port-city-battered-by-russias-invasion/
paramedics at a maternity hospital converted into a medical ward in Mariupol
basement of a maternity hospital converted into a medical ward and used as a bomb shelter, in Mariupol on Monday
lies on a stretcher in the corridor of a maternity hospital converted into a medical ward in Mariupol on Tuesday
which is counter to:
Russia had warned on 7 March that the hospital had been turned into a military location from which Ukrainians were firing.
https://news.sky.com/story/ukraine-war-russia-says-claim-it-bombed-childrens-hospital-is-fake-news-12562107