Either voluntary or involuntary. I'm also interested if it was common to move to a different type of unit as well (Ex: footman -> archer). Thank you!
This is a very wide question. Where are we, when?
Swedish medieval peasant militia was supposed to be both archers (later crossbowmen), spearmen and axemen/swordsmen (depending on region they came from) as the situation demanded it.
Engligt longbowmen at times fought in melee - they did at Agincourt, using led hammers and daggers to kill French knights who were stuck in the mud and had fallen over due to massive crowding.
Viking raiders usually looted whatever armour they could come over, and it was not unknown for men that had been rear rank spearmen to become front rank axemen/swordsmen as they acquired armour to take that role.
In the knightly orders, you could move up from sergeant to knight, but the role would be the same - on foot or on horseback.
Mongol and other steppe archers were multi-role - they could dismount to provide more stable fire, could go into melee with lance and sabre or remain horse archers, depending on the situation.
For mercenaries, it was common to be pressed into service with the victorious army if you were captured in battle or in a siege - and then you were given a role, usually at the bottom of the pecking order.
If you were pressed into a feudal levy or a captured mercenary, or a volunteer and it was discovered you had a rare or valuable skill, such as with a crossbow or a bow, being a siege engineer/skilled carpenter, or if you could ride a horse in battle, you could be sent to another unit.
Disease always made sure there were holes to fill in the ranks of any unit.