Thanks to Ulrike Fischer who pointed me in the right direction, I finally got it working, probably for all glyphs and all fonts. I think the code is even faster than the linked one (O(1) compared to O(N), but I'm a Lua beginner), and the linked one doesn't work for all glyphs, because not all glyphnames are in the table especially those with prefix uni are missing.
\documentclass[a4paper]{article}
\usepackage{fontspec}
\usepackage{luacode}
\begin{luacode}
-- the following code is for creating the ligature only; not for making it copyable/searchable
fonts.handlers.otf.addfeature{
name = "ligacustom",
type = "ligature",
data =
{
[utf.byte("Ђ")] = {utf.byte("T"), utf.byte("h")},
},
}
-- the following code is for debugging only
local dump = function(o)
-- source: https://stackoverflow.com/a/27028488
if type(o) == 'table' then
local s = '{ '
for k,v in pairs(o) do
if type(k) ~= 'number' then k = '"'..k..'"' end
s = s .. '['..k..'] = ' .. dump(v) .. ','
end
return s .. '} '
else
return tostring(o)
end
end
-- the following code is for making generic glyphs copyable/searchable as wanted; even from private use area
local patch_make_custom_glyphs_searchable_xits = function(fontdata)
if fontdata.fontname == "XITS-Regular" -- when the patch should only apply to XITS Regular font
then
-- for another font you can print the font name to console using print(fontdata.fontname)
-- add as many as you want; utf.byte("ß") is same as python3 ord("ß") for testing
fontdata.characters[utf.byte("Ђ")]["tounicode"] = {utf.byte("T"), utf.byte("h")}
fontdata.characters[utf.byte("ß")]["tounicode"] = {utf.byte("Ä"), utf.byte("Ä"), utf.byte("Ä")}
end
-- print(dump(fontdata.characters))
end
luatexbase.add_to_callback
(
"luaotfload.patch_font",
patch_make_custom_glyphs_searchable_xits,
"patch_make_custom_glyphs_searchable_xits"
)
\end{luacode}
\begin{document}
% setmainfont after the lua code!
\setmainfont[
RawFeature={+ligacustom},
]{XITS-Regular.otf}
Ђ The ß% copies in Sumatra PDF and Adobe Reader as "Th The ÄÄÄ" using "XITS-Regular.otf"
\end{document}

["uni0402"] = {84, 104}. – Ulrike Fischer Nov 15 '21 at 16:40Uis always just"U"so it should work, but does only withU.sc. But I don't know why it even fails on Source Serif for non small caps... E: according to the lua file the names are ok... – Guest Nov 15 '21 at 19:39XITS-Regular.otffont, but I'm using the correct names. – Guest Nov 15 '21 at 19:53\textsc{...}. But when the document has common unicode chars likeAorЂit's just ignored. I validated this using prints to console using this: https://stackoverflow.com/a/27028488 – Guest Nov 17 '21 at 21:59