When you spawn a unit in a province under attack that is defended by a hero, the unit does not join the rest of the army, it simply stays out of the battle. Surely if the enemy moves his troops or uses the attack order, they would enter into combat with that unit that was spawned and left alone, and obviously it would be lost.
The unit should be integrated into the rest of the army, I don't think the developers thought of doing this on purpose, but if the answer is yes it is thought like that, I would say that it is unfair and not ideal.
Screenshot:
The enemy army attacks my troops, then the airship is finished being produced and is not integrated into the rest of the army (it remains out of the battle).