The nameless man was killed because of the evidence we have from sources. He was not innocent. The Makkans persecuted the Muslim community for many years and now a spy appeared trying to collect information so he can give back to the enemy and they used that information to attack the Muslims.
Narrated Salama bin Al-Akwa: “An infidel spy came to the Prophet while he was on a journey. The spy sat with the companions of the Prophet and started talking and then went away. The Prophet said (to his companions), ‘Chase and kill him.’ So, I killed him.” The Prophet then gave him the belongings of the killed spy (in addition to his share of the war booty). – (Bukhari Volume 4, Book 52, Number 286)
Ibn Salamah bin Al Akwa’ repoted on the authority of his father. A spy of the polytheists came to the Prophet when he was on a journey. He sat near his Companions and then slipped away. The Prophet said “look for him and kill him”. He said “I raced to him and killed him. I took his belongings which he (the Prophet) gave me. – (Sunan Abi Dawud Book 14, Hadith 2647)