Actually, for the most part, coffins AREN’T (and WEREN’T) generally shaped that way. It’s just a ‘cartoon’ or iconic media image.
In some times and places, materials were scarce (recall that labor-intensive vs. material intensive methods were the rule until sometime in the 20th century, and are often still the rule in the Third World). Space may have been a facto in some cases (the coffin shape you refer to is an elongated hexagon which can be tessellated or tiles on a flat place with no gaps, and is nore humanoid in form and therefore more efficient than a rectangular casket). but rectangular burial cases, bone boxes, etc. have been the most common rigid form for thousands of years (wrapping in burial cloths was more common in most of history).
In the US, the nature of funerals changed dramatically during the Civil War. This was the first time bodies were sent home from the battle, and the funeral industry bloomed under the influx of bodies (The four years of the US Civil War had 10x the soldier fatalities of the much longer Vietnam War, and more than WWI as well. These deaths were dwarfed by the civilian casualties from hunger, disease, etc. in the disruption caused by the war.)
The Union and Confederate Armies, strapped for cash and flush with cheap soldier labor, might have chosen to save a few buck on the hundreds of coffinc it made a day. Even then, most bodies were sent home in rectangular boxes, not tapered coffins, because they could be slapped together easily by unskilled labor, especialy after heavy battles. Naturally, these shoddy boxes gave boxes a bad name, and funeral directors pushed for ‘classier’ coffins when the family could afford them, even though the bodies came home in a pine box.
I would guess that the coffin shape you describe is mostly a fashion artifact of the Civil War and postwar era in the US (and other eras elsewhere). I have seen far more rectangular caskets in history, from the pre-Christian Middle East to the Roman Empire to the present-day US/Europe. In Asia, a variety of shapes were used, but the rectangular was quite common, probably the most common, for the simple ease of cinstruction
There is probably some psychological influence from (e.g.) the famous sarcophagi of the Pharoahs, etc. (Though the Egyptians used square caskets, and indeed the outer case of the Pharoah’s sarcophagi were rectangular, the public image is of the more tapered inner cases.