Whether it is technically illegal or not doesn’t really matter, more so that it’s enforced — Thanks.
Historians have recently called attention to the performative nature of Tokugawa rule: the facade or surface of Tokugawa supremacy was more important than the reality. So lords of domains had quite a lot of autonomy and therefore policies varied greatly from region to region. But in general peasants were restricted to their town or village and could only travel outside of the domain in unusual circumstances.
Peasants did sometimes engage in commercial activities but it was somewhat unusual for them to change their official familial occupation from farmer to merchant--though such changes did happen and can be found in the documentary record.
Highways were tightly controlled and travel along them was regulated by checkpoints at which travelers needed to show documents with official approval for sanctioned travel: alternate service in Edo, travel for business reasons, approved family travel, or religious pilgrimage.
Over time commoners learned to manipulate the system and found various ways to travel for pleasure or to otherwise break the rules.
Some good scholarly sources for further reading:
Luke Roberts, Performing the Great Peace: Political Space and Open Secrets in Tokugawa Japan - about the layered quality of early modern Japanese politics
Amy Stanley, Stranger in the Shogun's City: A Japanese Woman and Her World - about the daughter of a Buddhist priest from rural Japan who escapes to Edo and makes a life for herself there
Constantine Vaporis, Breaking Barriers: Travel and the State in Early Modern Japan - about the system of travel regulation
Constantine Vaporis, Tour of Duty: Samurai, Military Service in Edo, and the Culture of Early Modern Japan - about the culture of travel that the system of alternate attendance produced
Laura Nenzi, Excursions in Identity: Travel and the Intersection of Place, Gender, and Status in Edo Japan - narratives of how diverse Japanese, particularly women, escaped hierarchies and family confined to find meaning in travel
Japan didn't (legally) divide society into peasants and merchants and artisans, but into "commoners" who lived in the countryside, and "townsfolk" who lived in the cities.
With that said, answer to the question is "yes". As /u/mpitelka touched upon, the Edo Bakufu (and in fact the domains as well) seem to only care about maintaining a facade that its rules were being followed, rather than making sure the rules actually were followed. With regard to the subject at hand, it seems that the government pretty much cared only that the documentation was filed properly, not that the documentation was correct, unless the offending party breaks other laws. This results in a large amount of people holding double (or triple) identities, changing based on what they were currently doing and switching back and forth as appropriate. And even when this was brought up, it seems the government was completely fine with it. This means of course that we do have surviving cases of someone who held the town-merchant identity at the same time he held a village-farmer identify. For instance, Jūbei, a peasant from Ōmi, was the same person as Ōmiya Chūeimon (Chūeimon of the Ōmi-store), a name he used when running his store in Mikawa. As long as he's documented as being a peasant when he's doing peasant things, and being a merchant when running his store, and broke no other laws, even though double identity should be illegal the government seem to have turned a blind eye. There's even a case of a peasant who had two different merchant names. Born Kahei, he was Seishichi the peasant of a small hamlet, Ōtsuya Seishichi when he sold cotton in a local village, and Kiya Sakujūrō when being a rich rice merchant in Ōtsu City. This came about because Chōshichi, his uncle once-removed used to work for Kiya as a servant, and ended up inheriting the business. Chōshichi died without a son, and so passed the Kiya Sakujūrō position to his nephew, Kahei's father, Seishichi, who passed the Seishichi title to Kahei to become Kiya Sakujūrō. When the father died, the Kiya Sakujūrō position was supposed to be passed to Kahei's younger brother. But then a boatsmen family adopted his first younger brother. To prevent double identity, either Seishichi or Kiya was supposed to go to his second younger brother on the latter's maturity, but he died and then Kahei ended up holding on to both identities. This didn't become a problem until the lord of Kahei's hamlet tried to raise some money in 1825. Kahei tried to get out of it by saying he was Kiya Sakujūrō of Ōtsu City, not Seishichi the peasant. The authorities then tracked down proof that he was effectively both at once, but let him get away with it if he paid up the originally requested taxes. Kahei ended up passing the Seishichi identity to someone else (possibly another relative) in 1827.
Kahei's story is perhaps illustrative on how such a thing often happened. In the Edo period there were considerable numbers of migrant workers throughout Japan, especially in the agricultural "off" season of winter. The men of Echigo and Aizu both crossed the mountains to got themselves employed building houses in the Kantō. The men in the villages of Kudoyama, Tajima went to sell udon in Kyōto. Rice wine making was also another common area migrant workers went to get employment in. And of course we can't forget the common employment as a general labourer or a servant for a samurai or merchant family. A common saying on the streets of Edo around 1800 was apparently "a rice miller in Echigo was a maid in Sagami, a rice miller in Echigo was a sansuke (samurai's servant) in Noto, a rice miller in Echigo was a jailer in Echizen." The Bakufu tried to limit the men that flowed into Edo for employment by requiring licenses, and a couple of times issued orders for them to return to their villages, but to no avail. An investigation in 1843 found that of 31,888 migrant workers, only 414 (1.3%) had licenses.
For sure that most of these would have been poorer people looking for opportunity. But, like Chōshichi and Kahei (Seishichi) demonstrate, given quite a bit of luck, with the most common being adoption by the family you were employed with, you could actually do very well for yourself. Even becoming a samurai was not impossible. Kondō Isami, the leader of the Shinsengumi, was born a peasant.