And the best, and certainly the cheapest, way to achieve that is by having happy employees in a healthy work environment. So in my experience, that's what HR tries to achieve.
I'm sorry your experience was different.
I think that's way too broad and general of a statement. It really depends on the nature of the industry, the competitive landscape, and many other things. In non-essential software, where the hands-on technical employees have tremendous influence over the direction of the firm, that may be true. In extractive industries and possibly even industrial ones it may be cheaper to externalize as many costs as possible, invest the bare minimum legally required in safety equipment and training, etc.
Why did it take strikes, literal shooting battles, legislative action, and generally tremendous amounts of effort to enact basic workers rights in this country if it would have been cheaper to just ... give people a happy and healthy working environment?
It's generally not the cheapest, and often not even possible where there is a deep conflict between preferences of employees and/or between the fundamental market realities of the line of business and ideals of “healthy work environment“.
It's true that “cattle not pets” isn't the line you give to the cattle, who really want to believe that they are “valued members of the corporate family“.
Their function within capitalist industry is directly, fundamentally, and uniquely concerned with the manner in which capitalist industry itself is hostile to workers.
A firm in which HR is not generally hostile to the interests of workers is fundamentally maladapted to competition in a capitalist economy and is, ceteris paribus, likely to either fail in the face of competition without this defect or be forced to rectify the defect.