To understand the origins of this prohibition it seems best to go back to when it was first put in writing and its context there.
https: //ijms.nmdl.org/article/download/15891/10023) - (url misformatting is intentional, this is a download only link.)
Throughout history, opposing nations have established ground rules for war, but until the
nineteenth century these rules applied only to a particular conflict and the countries
involved. With the 1864 Geneva Conventions, rules of war became international. Dating
from the Middle Ages, “a knight always trusted the word of another knight, even if he
were an enemy. Perfidy was considered a dishonor which could (never) be redeemed.” 59
The use of false national flags in terrestrial
warfare was considered a perfidy and consequently banned by Section II, Chapter 1,
CORIOLIS Volume 5, Number 2, 2015[Type text] Page 58
Article 23 of the Hague Convention With Respect To the Laws And Customs of War On
Land (Hague II)(29 July 1899) Since the flying of false colors ruse has been barred for over a century by international law with respect to terrestrial warfare.
The link above is mostly meant to provide some context to Hague's introduction.
Hague 1899 Article 23:
Article 23
Besides the prohibitions provided by special Conventions, it is especially prohibited:--
To employ poison or poisoned arms;
To kill or wound treacherously individuals belonging to the hostile nation or army;
To kill or wound an enemy who, having laid down arms, or having no longer means of defence, has surrendered at discretion;
To declare that no quarter will be given;
To employ arms, projectiles, or material of a nature to cause superfluous injury;
To make improper use of a flag of truce, the national flag, or military ensigns and the enemy's uniform, as well as the distinctive badges of the Geneva Convention;
To destroy or seize the enemy's property, unless such destruction or seizure be imperatively demanded by the necessities of war.
As others have said, the requirements to spare civilians and also to treat enemy POWs humanely have, almost as corollary, the need to clearly identify one's troops and that is certainly a motivation.
But, article 23 in general seems to aim to make warfare more "gentlemanly" and humane and this is where the original intent seems to have been.
If you recall your WW1 history, there were initially all sorts of recently-introduced provisions - Hague 1899, 1907 - that were supposed to outlaw certain classes of weapons and behaviors, many of which were quickly discarded:
- no mine warfare
- restrictions on submarine warfare, such as subs hailing before firing and taking in shipwrecked sailors and passengers
- poison gas
After WW1 some of them were brought back in, as unrestricted warfare doesn't really benefit anyone: poison gas for example is initially an advantage to the attacker, but quickly makes everyone miserable once soldiers wear NBC gear.
Likewise, the prohibition to fly false colors (which is treated somewhat differently in sea warfare), seems to be a generally good idea, giving benefits to all, while a breach is not all that advantageous.
As wikipedia states, covering this under perfidy:
In the context of war, perfidy is a form of deception in which one side promises to act in good faith (such as by raising a flag of truce) with the intention of breaking that promise once the unsuspecting enemy is exposed (such as by coming out of cover to take the "surrendering" prisoners into custody).
Perfidy constitutes a breach of the laws of war and so is a war crime, as it degrades the protections and mutual restraints developed in the interest of all parties, combatants and civilians.
Hague got carried over into the Geneva conventions, which are mostly about humanitarian laws, but also very much concerned with rules of warfare in general:
Red Cross
International humanitarian law (IHL) is
a set of rules that seek for humanitarian
reasons to limit the effects of armed
conflict. IHL protects persons who are
not or who are no longer participating in
hostilities and it restricts the means and
methods of warfare. IHL is also known
as the law of war and the law of armed
conflict.