Praise be to Allah.
The basic principle is that writing on the grave is haraam and is not permissible. See the answer to question no. 9986.
But some of the scholars (may Allah have mercy on them) are of the view that it is permissible to write the name only in case of necessity.
It says in al-Mawsoo‘ah al-Fiqhiyyah (32/252): The fuqaha’ also differed with regard to writing on the grave. The Maalikis, Shaafa‘is and Hanbalis are of the view that it is makrooh to write on the grave in all cases, because of the hadeeth of Jaabir who said: The Prophet (blessings and peace of Allah be upon him) forbade plastering over graves, sitting on them, erecting structures over them and writing on them. The Hanafis and al-Subki among the Shaafa‘is were of the view that there is nothing wrong with writing on it if there is a need for that so that the location will not be lost and so that the grave will not be treated with disrespect. End quote.
Shaykh Ibn ‘Uthaymeen (may Allah have mercy on him) said: (The issue of) writing on the grave is subject to further discussion. There is nothing wrong with writing which is intended only to confirm the name and indicate that this is a grave. As for writing which is similar to what was done during the Jaahiliyyah, such as writing the person’s name accompanied by a eulogy, stating that he did such and such and other words of praise, or writing lines of poetry – this is haraam.
That includes what some ignorant people do, namely writing Soorat al-Faatihah, for example, or other lines on a stone that is placed on the grave. All of that is haraam and the one who sees it in the graveyard should remove this stone, because this comes under the heading of evil that must be changed. And Allah is the source of strength.
End quote from Sharh Riyadh as-Saaliheen
Shaykh Hamad ibn ‘Abdullah al-Hamad (may Allah preserve him) said: But is it permissible to do that (putting the name on the grave) as a sign if the family of the deceased cannot put a marker on it because there are so many graves and there is no way to tell them apart except by means of writing? Some of the scholars are of the view that that is permissible and that if only the name is written, there is nothing wrong with it, so long as no other marker will serve that purpose. And that is because there is a need to know the grave of the deceased.
It is proven in Sunan Abi Dawood that the Prophet (blessings and peace of Allah be upon him) put a rock at the head of ‘Uthmaan ibn Maz‘oon (i.e., on his grave) and said: “So that I may know the grave of my brother thereby, and I will bury near him those who die of my family.”
This is a good opinion, in sha Allah.
End quote from Sharh az-Zaad
And Allah knows best.