I managed to set a custom resolution using ADB over wifi (part of the Android developer tools) with the following command:
I did try to set it to 4k, but 2880 seems to be the maximum width I can set, so I just set the height as 1620 to optimise it for my 16:9 aspect ratio monitor.
Technically, you could try to calculate the highest resolution with a value equal to or less than 2880 which would match your displays aspect ratio, whch may be useful if you have a 4:3, 16:10 or 21:9 aspect ratio display.
Once changed, you can check the new resolution by entering the following command and reading the 'Override' value, the 'Physical' size is just the phones screen resolution so ignore that.
There may be some visual bugs when changing the resolutions as they obviously haven't been officially tested.
You can change the DPI by modifying the density value, by default it is 160.
Lower values will shrink the UI to display more stuff on screen, the higher density makes items look bigger on screen.
Changing the density multiple times may cause display issues such as icons not displaying correctly etc...
I fixed this just by selecting a different resolution in the DeX settings to quickly restore the devices default setttings.
*IMPORTANT NOTE*
This will almost certainly create problems when going back into phone mode unless you apply the phones resolution and DPI settings before exiting DeX and the ADB tool.
Please note that I can't integrate this into Desktop Hub as this adjusts a secure system setting which requires you to use the ADB commands, which are not available to third party apps i.e. Desktop Hub.