When I create a pdf file containing English text using pdflatex, I can copy text from it and get a reasonably fair text when pasted in a text editor. But when I do the same with a pdf of Malayalam text created using xelatex, the pasted text has several gaps and often wrong characters too. I was under the impression that the pdf created with xelatex will also have Unicode text, which then can be searched, indexed and copied. At least, that is the impression I got from reading about xelatex. On the other hand, I found that Malayalam text can be clearly copied from a pdf created from Libre Office Writer. Is my impression about XeLaTeX wrong, or my understanding of the pdf format wrong? Or am I mistaken somewhere else? I would be very grateful if someone knowledgeable about this can give a reply.
Malayalam is a script related to Sanskrit, and the single characters undergo considerable changes from the input to the output. In particular, some vowels are placed before the corresponding consonant, and glyphs are put together into compounds, so called ligatures. See https://en.wikipedia.org/wiki/Malayalam_script for examples of ligatures and vowel placements (and much more!).
On the font side, these compounds of consonants are usually precomposed, that is, drawn separately to make them look right. During the typesetting, the characters are replaced with the pre-composed codepoints in the fonts, but those often don't have a textual representation.
There are ways around is, and the font can declare a mapping from code points to character sequences, but this is often not available.
This seems to be a technical limitation of the XeLaTeX engine. The issue is incomplete CMAP (character map) of the font embedded in PDF. Recent versions of
luahbtex can generate PDF containing complex script texts which can be copy pasted back to a text editor properly. Here is one example of such a document: http://books.sayahna.org/ml/pdf/bbh-web.pdf