This should be possible but requires java code. Be aware that a solution like that is very tricky! What kind of flexibility do you want to achieve?
You can build a custom Java action which takes the XPath stored in a String as an input and outputs entities. You can also build the XPath as a string in the Java class, then execute the XPath query in the Java action.
Example Java code below contains string 'xPath' and executes it, then returns the results back to your Mendix application:
return Core.retrieveXPathQuery(getContext(), xPath);