On defense, the stack's strongest unit against the attacker will always be selected.
On attack, you can select whichever unit you like for the attack.
What would be interesting might be if there were a way to let great leader units pick which unit in a stack that they wish to attack, or have this capability as a promotion. I don't think it's possible with XML.
Sharing hit points ala civ3 armies? You could simulate sort of simply by giving all units 100 percent chance for withdrawal, but that imbalances against mounted units. Maybe let great military leaders build a building that gives a promotion giving this, or maybe 50 percent. No way to bind units together. I guess you could probably script it some way, but why? To make great military leaders more meaningful?
If that's what you want how about just giving the military leader unit itself a special free promotion that does all the "all units same stack" and "all units within one square" benefits. Healing and such.