Oct 2022 macOS 12.6 Solution
Adding another solution to the list. Imron Gamidli's solution got me the closest to what I wanted with the toggle behavior.
Here is my script which I've added as a Raycast command so that I can trigger from raycast or assign a keyboard shortcut.
#!/usr/bin/osascript
# Required parameters:
# @raycast.schemaVersion 1
# @raycast.title Toggle Resolution
# @raycast.mode silent
# Optional parameters:
# @raycast.icon š¤
set monitor to 2
tell application "System Preferences"
activate
set the current pane to pane id "com.apple.preference.displays"
delay 2
tell application "System Events"
tell window "Displays" of application process "System Preferences"
click button "Display Settingsā¦"
delay 2
click row monitor of outline 1 of scroll area 1 of sheet 1
click radio button "Scaled" of radio group 1 of sheet 1
set elems to entire contents of UI element 6 of sheet 1
try
set selected_button to button "Resolution5, Selected" of UI element 6 of sheet 1
click button "Resolution1" of UI element 6 of sheet 1
on error
click button "Resolution5" of UI element 6 of sheet 1
end try
delay 0.5
click button "Done" of sheet 1
end tell
end tell
end tell
# The next line is optional and could be commented out by prepending with a hash (#).
delay 2
quit application "System Preferences"